package org.sixdog.mcsit.service.impl;

import com.wf.captcha.SpecCaptcha;
import org.sixdog.mcsit.service.CaptchaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class CaptchaServiceImpl implements CaptchaService {




    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;
    @Autowired
    private JavaMailSender mailSender;


    @Override
    public boolean saveCaptcha(String key, String captcha,long durationSec) {
        redisTemplate.opsForValue().set(key,captcha);
        Boolean result = redisTemplate.expire(key, durationSec, TimeUnit.SECONDS);
        return result;
    }

    @Override
    public boolean equalsCaptcha(String key, String captcha) {
        String value =(String) redisTemplate.opsForValue().get(key);
        //进行比较验证码，不区分大小写
        if (value != null && value.equalsIgnoreCase(captcha) ){
            return true;
        }
        return false;
    }

    @Override
    public long getCaptchaTimeByKey(String key) {
        long captchaTime=redisTemplate.opsForValue().getOperations().getExpire(key);
        return  captchaTime;
    }

    @Override
    public Map<String,String> buildCaptcha(String captchaType, Map<String, String> args) {
        String captchaKey = UUID.randomUUID().toString();
        switch (captchaType){
            case DEFAULT_TYPE : {
                SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 5);
                String captchaValue = specCaptcha.text().toLowerCase();
                saveCaptcha(captchaKey,captchaValue,61);
                String captchaImg = specCaptcha.toBase64();
                Map<String,String> data = new HashMap<>();
                data.put("captchaKey",captchaKey);
                data.put("captchaImg",captchaImg);
                return data;
            }
            case EMAIL_TYPE : {
                String email = args.get("email");
                String ip  = args.get("ip");
                SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 6);
                String captchaValue = specCaptcha.text().toLowerCase();
                //发邮件
                try {
                    SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
                    simpleMailMessage.setFrom("SixDog-McSit" + "<1773374180@qq.com>");
                    simpleMailMessage.setTo(email);
                    simpleMailMessage.setSubject("注册账号通知");
                    simpleMailMessage.setText("您的验证码为：\n\t\t"+captchaValue);
                    mailSender.send(simpleMailMessage);
                    //保存验证码到redis数据库
                    boolean isEmailExists = saveCaptcha(email,captchaValue,600);
                    boolean isIpExists = saveCaptcha(ip,captchaValue,60);

                    if (isEmailExists && isIpExists){
                        Map<String,String> data = new HashMap<>();
                        data.put("success","success");
                        return data;
                    }

                }catch (Exception e){
                    System.out.println(e);
                }
                break;
            }
        }
        return null;
    }


}
